package cn.doctor.assistant.pad.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.os.Looper;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

import java.io.Serializable;

import cn.doctor.assistant.pad.MyApplication;
import cn.doctor.assistant.pad.im.utils.ShowBigImage;

public class UI {

	public static void enter(Activity activity) {
		MyApplication.getInstance().enter(activity);
	}

	public static void startActivity(Class<? extends Activity> clazz) {
		Activity activity = MyApplication.getInstance().getCurrentActivity();
		activity.startActivity(new Intent(activity, clazz));
	}

	public static void startActivity(Class<? extends Activity> clazz, String key, int value) {
		Activity activity = MyApplication.getInstance().getCurrentActivity();
		activity.startActivity(new Intent(activity, clazz).putExtra(key, value));
	}

	public static void startActivity(Class<? extends Activity> clazz, String key, String value) {
		Activity activity = MyApplication.getInstance().getCurrentActivity();
		activity.startActivity(new Intent(activity, clazz).putExtra(key, value));
	}

	public static void startActivity(Class<? extends Activity> clazz, Object... keyValueExtra) {
		Activity activity = MyApplication.getInstance().getCurrentActivity();
		Intent intent = new Intent(activity, clazz);

		if(keyValueExtra.length > 0 && keyValueExtra.length % 2 != 0){
			throw new UnsupportedOperationException("keyValueExtra 必须是偶数");
		}

		for (int i = 0; i < keyValueExtra.length; i++) {
			String key = (String) keyValueExtra[i];
			Object value = keyValueExtra[++i];
			if (value instanceof Integer) {
				intent.putExtra(key, (int) value);
			} else if (value instanceof Boolean) {
				intent.putExtra(key, (boolean) value);
			} else if (value instanceof String) {
				intent.putExtra(key, (String) value);
			} else if (value instanceof Long) {
				intent.putExtra(key, (long) value);
			} else if (value instanceof Double) {
				intent.putExtra(key, (double) value);
			} else if (value instanceof Float) {
				intent.putExtra(key, (float) value);
			} else if (value instanceof Serializable) {
				intent.putExtra(key, (Serializable) value);
			} else {
				// unsupported type...
				throw new UnsupportedOperationException("不支持的数据类型:" + value);
			}

		}
		activity.startActivity(intent);
	}

	public static View inflateView(int resId) {
		return LayoutInflater.from(MyApplication.getInstance().getCurrentActivity()).inflate(resId, null);
	}

	public static View inflateView(int resId, ViewGroup root) {
		return LayoutInflater.from(MyApplication.getInstance().getCurrentActivity()).inflate(resId, root);
	}

	public static void showToast(String msg) {
		final Activity context = MyApplication.getInstance().getCurrentActivity();

		if (Looper.myLooper() == context.getMainLooper()) {
			Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
		} else {
			final String m = msg;
			context.runOnUiThread(new Runnable() {

				@Override
				public void run() {
					Toast.makeText(context, m, Toast.LENGTH_SHORT).show();
				}
			});
		}
	}

	public static void hideKeyboard() {

		InputMethodManager inputManager = (InputMethodManager) MyApplication.getInstance().getCurrentActivity()
				.getSystemService(Context.INPUT_METHOD_SERVICE);

		View view = MyApplication.getInstance().getCurrentActivity().getCurrentFocus();

		if (view != null) {
			inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
		}
	}

	public static void showKeyboard(EditText editText) {
		editText.setFocusable(true);
		editText.setFocusableInTouchMode(true);
		editText.requestFocus();
		InputMethodManager inputManager = (InputMethodManager) editText.getContext()
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		inputManager.showSoftInput(editText, 0);
	}

	/**
	 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
	 */
	public static int dip2px(float dpValue) {
		int value = 0;
		try {
			Context context = MyApplication.getInstance().getCurrentActivity();
			value = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue,
					context.getResources().getDisplayMetrics());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return value;

	}

	// 字体大小转换
	public static float sp2px(float spValue) {
		Context context = MyApplication.getInstance().getCurrentActivity();
		return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spValue,
				context.getResources().getDisplayMetrics());
	}

	//显示大图
	public static void showBigImage(String imageUrl) {
		if (!TextUtils.isEmpty(imageUrl)) {
			Activity context = MyApplication.getInstance().getCurrentActivity();
			Intent it = new Intent(context, ShowBigImage.class);
			it.putExtra("localpath", imageUrl);
			context.startActivity(it);
			context.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
		}
	}

	public static Point getScreenWidthHeight() {
		Point p = new Point();
		DisplayMetrics dm = new DisplayMetrics();
		MyApplication.getInstance().getCurrentActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
		p.x = dm.widthPixels;
		p.y = dm.heightPixels;
		return p;
	}

	public static int getScreenWidth() {
		DisplayMetrics dm = new DisplayMetrics();
		MyApplication.getInstance().getCurrentActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
		return dm.widthPixels;
	}

	public static int getScreenHeight() {
		DisplayMetrics dm = new DisplayMetrics();
		MyApplication.getInstance().getCurrentActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
		return dm.heightPixels;
	}

	public static int dip2px(Context context, float dipValue){
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int)(dipValue * scale + 0.5f);
	}

	public static int px2dip(Context context, float pxValue){
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int)(pxValue / scale + 0.5f);
	}

	public static int sp2px(Context context, float spValue) {
		final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
		return (int) (spValue * fontScale + 0.5f);
	}
}
